package com.zoro.barn.commons.tools.string;

import org.apache.commons.lang3.StringUtils;

/**
 * @author zhaoxingwu
 */
public class StringTools {

    public static String trim(String source, String s) {
        if (StringUtils.isAnyBlank(s,source)) {
            return source;
        }

        int length = s.length();
        String result = source;
        while (result.startsWith(s)) {
            result = result.substring(length);
        }
        while (result.endsWith(s)) {
            result = result.substring(0, result.length() - length);
        }

        return result;
    }

    /**
     * 获取url参数中的值
     *
     * @param queryString url参数
     * @param key         参数名
     * @return value
     */
    public static String getParamFromQueryString(String queryString, String key) {
        if (StringUtils.isBlank(queryString) || StringUtils.isBlank(key)) {
            return null;
        }

        String[] parameters = queryString.split("&");
        if (parameters.length > 0) {
            for (String p : parameters) {
                String[] pair = p.split("=");
                if (pair.length <= 1) {
                    return null;
                }
                if (StringUtils.equals(key, pair[0])) {
                    return pair[1];
                }
            }
        }
        return null;
    }
}
